package at.jaki.ubb.crawling.stlib.dataFinder;

import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import at.jaki.ubb.crawling.model.DataFinderResult;
import at.jaki.ubb.crawling.model.Target;
import at.jaki.ubb.crawling.stlib.utils.WebPageMinerUtils;
/**
 * uses Google translate to translate a text.
 * <h1>Does not work</h1>
 * 
 * @author Attila Jakabfi
 *
 */
@Deprecated
public class GoogleTranslater extends SimpleHttpClient {

	private static final Logger LOG = Logger.getLogger(GoogleTranslater.class);

	public static final String INPUT_TYPE = "translate";

	private static final String GOOGLE_TRANSLATE_URL = "translate.google.com/";

	/**
	 * parameters: hostL, text, targetL
	 */

	@Override
	public DataFinderResult<String, String> findData(Target<String> t) {
		String word = null;
		try {
			word = WebPageMinerUtils.createURI(GOOGLE_TRANSLATE_URL,null, getParams(t.getValue(), "ro", "en"));
		} catch (URISyntaxException e) {
			LOG.error(e);
			return new DataFinderResult<String, String>(null, null);
		}
		String page = getPage(word);
		String trWord = getTrResult(page);
		LOG.debug("translated "+t.getValue()+" to "+trWord);
		return new DataFinderResult<String, String>(null, null);
	}

	private String getTrResult(String page) {
		Pattern p = Pattern.compile("[<]span id[=]result_box class[=][\"]short_text[\"][>][<]span[\\S\\s]*?title[=][\"](.*?)[\"]");
		Matcher matcher = p.matcher(page);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return null;
	}

	private Map<String, String> getParams(String value, String hostL, String targetL) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("hl", hostL);
		map.put("tl", targetL);
		map.put("text", value);
		return map;
	}

	@Override
	public void init() {
	}

	@Override
	public Collection<String> getTypes() {
		return Collections.singletonList(INPUT_TYPE);
	}

}
